﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using Microsoft.International.Converters.PinYinConverter;

namespace orderCreater
{
    public class tool
    {
        public tool() { }

        public string CreateOrderCode()
        {
            string sn = string.Empty;
            string r1 = string.Empty;
            string r2 = string.Empty;
            Random ra = new Random();
            r1 = ra.Next(10000, 99999).ToString();
            r2 = ra.Next(10000, 99999).ToString();
            sn = new string(DateTime.Now.ToFileTimeUtc().ToString().ToCharArray().OrderBy(s => Guid.NewGuid().ToString()).ToArray());
            sn = r1.Substring(0, 3) + sn.Substring(0, 4) + r2.Substring(0, 4) + sn.Substring(5, 5);
            return sn;
        }
        public string createPayCode(string memberId) 
        {
            string sn = string.Empty;
            string r1 = string.Empty;
            string r2 = string.Empty;
            Random ra = new Random();
            r1 = ra.Next(10000, 99999).ToString();
            r2 = ra.Next(10000, 99999).ToString();
            sn = new string(DateTime.Now.ToFileTimeUtc().ToString().ToCharArray().OrderBy(s => Guid.NewGuid().ToString()).ToArray());
            sn = r1.Substring(0, 3) + sn.Substring(0, 4) + r2.Substring(1, 3) + sn.Substring(5, 5) + sn.Substring(3, 3);
            return sn.Substring(0, 18 - memberId.Length) + memberId;
        }
        public string GetPinyin(string str)
        {
            string r = String.Empty;
            foreach (char obj in str)
            {
                try
                {
                    ChineseChar chineseChar = new ChineseChar(obj);
                    string t = chineseChar.Pinyins[0].ToString();
                    r += t.Substring(0, t.Length - 1);
                }
                catch (Exception)
                {

                    r += obj.ToString();
                }
            }
            r = r.ToLower();

            Random rd = new Random();
            string[] arrUserName = new string[] { "00", "5", "@", ".", "#", "2", "88", "&", "o", "168" };
            int rdKey = rd.Next(0, 10);

            r = r + arrUserName[rdKey] + rd.Next(1111, 9999).ToString();
            return r;
        }
        public string getRandEmail(string name) 
        {
            string rtn = "";

            Random rd = new Random();

            int key = rd.Next(0,17);
            
            rtn = name;

            switch (key)
            {
                case 0: rtn = rtn + "@126.com"; break;
                case 1: rtn = rtn + "@163.com"; break;
                case 2: rtn = rtn + "@gmail.com"; break;
                case 3: rtn = rtn + "@yeah.net"; break;
                case 4: rtn = rtn + "@sina.com"; break;
                case 5: rtn = rtn + "@qq.com"; break;
                case 6: rtn = rtn + "@tom.com"; break;
                case 7: rtn = rtn + "@eyou.com"; break;
                case 8: rtn = rtn + "@21cn.com"; break;
                case 9: rtn = rtn + "@yahoo.com.cn"; break;
                case 10: rtn = rtn + "@56.com"; break;
                case 11: rtn = rtn + "@live.com"; break;
                case 12: rtn = rtn + "@163.net"; break;
                case 13: rtn = rtn + "@3721.net"; break;
                case 14: rtn = rtn + "@aim.com"; break;
                case 15: rtn = rtn + "@walla.com"; break;
                case 16: rtn = rtn + "@inbox.com"; break;
            }

            return rtn;
        }
        public string CreatePayType()
        {
            string[] arrUserName = new string[] { "alipay", "alipay", "bankremit", "bankremit", "bankremit", "alipay", "bankremit", "bankremit", "alipay", "bankremit", "bankremit" };
            Random rd = new Random();
            int rdKey = rd.Next(0, 10);
            return arrUserName[rdKey];
        }
        public string IsPay()
        {
            string[] arrUserName = new string[] { "0", "1", "1", "1", "0", "1", "1", "1", "1", "0" };
            Random rd = new Random();
            int rdKey = rd.Next(0, 10);
            return arrUserName[rdKey];
        }
        public string shiDuan()
        {
            string[] arrUserName = new string[] { "2", "2", "1", "1", "1", "1", "1", "1", "1", "3" };
            Random rd = new Random();
            int rdKey = rd.Next(0, 10);
            return arrUserName[rdKey];
        }
        public static string post(string url, string paramsStr)
        {
            try
            {
                //注意提交的编码 这边是需要改变的 这边默认的是Default：系统当前编码
                byte[] postData = Encoding.UTF8.GetBytes(paramsStr);

                // 设置提交的相关参数 
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                Encoding myEncoding = Encoding.UTF8;
                request.Method = "POST";
                request.KeepAlive = false;
                request.AllowAutoRedirect = true;
                request.ContentType = "application/x-www-form-urlencoded";
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR  3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
                request.ContentLength = postData.Length;

                // 提交请求数据 
                System.IO.Stream outputStream = request.GetRequestStream();
                outputStream.Write(postData, 0, postData.Length);
                outputStream.Close();

                HttpWebResponse response;
                Stream responseStream;
                StreamReader reader;
                string srcString;
                response = request.GetResponse() as HttpWebResponse;
                responseStream = response.GetResponseStream();
                reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
                srcString = reader.ReadToEnd();
                string result = srcString;   //返回值赋值
                reader.Close();
                return result;
            }
            catch
            {
                return "error";
            }
        }

        public string CreateOrderTime(string shortDateStr)
        {
            Random random = new Random();
            string str = this.shiDuan();
            //string str3 = this.sr_dt_time.Value.ToShortDateString();
            string str4 = random.Next(0, 0x10).ToString();
            switch (str)
            {
                case "2":
                    str4 = random.Next(0x13, 0x18).ToString();
                    break;

                case "3":
                    str4 = random.Next(0, 10).ToString();
                    break;
            }
            string str5 = random.Next(0, 20).ToString();
            string str6 = random.Next(0, 60).ToString();
            if (str5.Length < 2)
            {
                str5 = "0" + str5;
            }
            if (str6.Length < 2)
            {
                str6 = "0" + str6;
            }
            string[] textArray1 = new string[] { shortDateStr, " ", str4, ":", str5, ":", str6 };
            return string.Concat(textArray1);
        }
    }
}
